// subPages/integral/orders/orders.js
const app = getApp();
var util =  require('../../../utils/util.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = 'orders'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [{nav: '全部'},{nav: '待发货'},{nav: '待收货'},{nav: '已完成'}],
    currentTab: 0,
    status: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    loadMoreView = this.selectComponent("#loadMoreView")
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getOrderList()
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
          that.getOrderList()
        }
      }
    }
  },

  //获取订单数据数据
  getOrderList:function(){
    var that = this
    util.getAjax(`${app.globalData.apiUrl}/get_jf_order`,that.getOrderData,{p: p,num: num,type: 2,user_id: that.data.userId,status: that.data.status},that.orderDataErr)
  },

  getOrderData(res){
    var that = this
    console.log(res)
    if(res.data.list.length != 0){
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.orderList.concat(res.data.list)
      }
      for(var i in list){
        if(list[i].time){
          list[i].time = util.setTime2(list[i].time)
        }
        list[i].statusTxt = util.getOrderStatus(list[i].status)
      }
      that.setData({
        orderList: list
      })
    }else{
      that.setData({
        orderList: []
      })
    }
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取商品数据异常
  orderDataErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取订单失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getOrderList()
  },

  clickLoadMore: function(e) {
    this.getOrderList()
  },

  //选择导航栏
  switchNav(event){
    var that = this
    var cur = event.currentTarget.dataset.current;      
    if (that.data.currentTab == cur) {
      return false;
    } else {
      that.setData({
        currentTab: cur
      })
      that.setNav(cur)
    }
  },

  //设置导航
  setNav:function(current){
    var status = ''
    switch (current) {
      case 0:
        status = 10
        break;
      case 1:
        status = 1
        break;
      case 2:
        status = 2
        break;
      case 3:
        status = 3
        break;
    }
    this.setData({
      status: status
    })
    this.getOrderList()
  },

  //操作按钮
  operationBtn:function(e){
    var that = this
    var type = e.currentTarget.dataset.type    
    switch (type) {
      case '1':
        //提醒发货
        
        break;
      case '2':
        //确认收货
        util.getAjax(app.globalData.apiUrl + '/operation_community_jf_shop_order',function(res){
          console.log(res)
          if(res.data == 1){
            util.showToast('确认收货成功','success',2000)
            that.getOrderList()
          }
        },{id: e.currentTarget.dataset.id,type: 2})
        break;
      case '3':
        //删除
        
        break;
      case '0':
        //查看订单详情
        wx.navigateTo({
          url: '/subPages/integral/orderDetails/orderDetails?orderNum=' + e.currentTarget.dataset.ordernum,
        })
        break;
    }
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getOrderList()
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})